//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
namespace DotNetNuke.UI.Skins.Controls
{
	partial class Styles : UI.Skins.SkinObjectBase
	{
		private string _condition;
		private bool _isFirst = false;
		private string _name;
		private string _src;
		private bool _useSkinPath = true;
	    private string _media;

		public string Condition {
			get { return _condition; }
			set { _condition = value; }
		}
		public bool IsFirst {
			get { return _isFirst; }
			set { _isFirst = value; }
		}
		public string Name {
			get { return _name; }
			set { _name = value; }
		}
		public string StyleSheet {
			get { return _src; }
			set { _src = value; }
		}
		public bool UseSkinPath {
			get { return _useSkinPath; }
			set { _useSkinPath = value; }
		}
	    public string Media
	    {
	        get
	        {
	            return _media;
	        }
            set
            {
                _media = value;
            }
	    }
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			AddStyleSheet();
		}
		protected void AddStyleSheet()
		{
			Control objCSS = Page.FindControl("CSS");
			if (objCSS != null) {
				Control objCtrl = Page.Header.FindControl(ID);
				if (objCtrl == null) {
					string skinpath = string.Empty;
					if (UseSkinPath) {
						skinpath = ((Skin)this.Parent).SkinPath;
					}
					HtmlLink objLink = new HtmlLink();
					objLink.ID = Common.Globals.CreateValidID(Name);
					objLink.Attributes["rel"] = "stylesheet";
					objLink.Attributes["type"] = "text/css";
					objLink.Href = skinpath + StyleSheet;
                    if(Media != "")
                        objLink.Attributes["media"] = Media; //NWS: add support for "media" attribute
					if (IsFirst) {
						int iLink;
						for (iLink = 0; iLink <= objCSS.Controls.Count - 1; iLink++) {
							if (objCSS.Controls[iLink] is HtmlLink) {
								break;
							}
						}
						AddLink(objCSS, iLink, objLink);
					} else {
						AddLink(objCSS, -1, objLink);
					}
				}
			}
		}
		protected void AddLink(Control cssRoot, int InsertAt, HtmlLink link)
		{
			if (string.IsNullOrEmpty(Condition)) {
				if (InsertAt == -1) {
					cssRoot.Controls.Add(link);
				} else {
					cssRoot.Controls.AddAt(InsertAt, link);
				}
			} else {
				System.Web.UI.WebControls.Literal openif = new System.Web.UI.WebControls.Literal();
				openif.Text = string.Format("<!--[if {0}]>", Condition);
				System.Web.UI.WebControls.Literal closeif = new System.Web.UI.WebControls.Literal();
				closeif.Text = "<![endif]-->";
				if (InsertAt == -1) {
					cssRoot.Controls.Add(openif);
					cssRoot.Controls.Add(link);
					cssRoot.Controls.Add(closeif);
				} else {
					cssRoot.Controls.AddAt(InsertAt, closeif);
					cssRoot.Controls.AddAt(InsertAt, link);
					cssRoot.Controls.AddAt(InsertAt, openif);
				}
			}
		}
	}
}
